package logic

import (
	"context"
	"month/videosvr/video"

	"month/client/internal/svc"
	"month/client/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type EsSearchLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewEsSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EsSearchLogic {
	return &EsSearchLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *EsSearchLogic) EsSearch(req *types.EsSearchReq) (resp *types.Res, err error) {
	// todo: add your logic here and delete this line
	res, err := l.svcCtx.VideoRpc.EsSearch(l.ctx, &video.EsSearchReq{
		Index: req.Index,
		Word:  req.Word,
		Page:  req.Page,
	})
	if err != nil {
		return &types.Res{
			Code: -1,
			Msg:  err.Error(),
			Data: nil,
		}, nil
	}
	var list []map[string]interface{}
	for _, v := range res.Data {
		var lis = map[string]interface{}{
			"title":     v.Title,
			"userid":    v.Userid,
			"sub_title": v.SubTitle,
			"address":   v.Address,
			"img":       v.Img,
		}
		list = append(list, lis)
	}
	return &types.Res{
		Code: 200,
		Msg:  "success",
		Data: map[string]interface{}{
			"data":  list,
			"page":  res.Page,
			"count": res.CountPage,
		},
	}, nil
}
